package com.duoqin.syncassistant.sms;

import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.duoqin.syncassistant.Utils;

public class SmsInfoUtil {
    public static final String INTENT_ACTION_SMS = "sms";
    public static final String INTENT_EXTRA_SMS_PATH = "sms_file";
    public static final String SMS_FILE_NAME = "smsfile.txt";

    public static final String[] ORIGINAL_PROJECTION = new String[] {
    	"_id",
    	"thread_id",
    	"address",
    	"person",
    	"date",
    	"date_sent",
    	"protocol",
    	"read",
    	"status",
    	"type",
    	"reply_path_present",
    	"subject",
    	"body",
    	"service_center",
    	"locked",
    	"sub_id", // "phone_id",
    	"error_code",
    	"seen",
    	"expired",
    	"wap_push" //,
//    	"icc_id"
    };

    public static ArrayList<SmsData> getPhoneSms(Context context){
        ArrayList<SmsData> smsList = new ArrayList<SmsData>();
        Uri uri = Uri.parse("content://sms/");
        Cursor cusr = null;
        try {
            ContentResolver resolver = context.getContentResolver();
            cusr = resolver.query(uri, ORIGINAL_PROJECTION, null, null, null);
            while (cusr!=null && cusr.moveToNext()) {
                SmsData smsData = getSmsData(cusr);
                if(smsData != null)smsList.add(smsData);
            }
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            if(cusr != null)cusr.close();
        }
        return smsList;
    }
    public static SmsData getSmsData(Cursor cusr){
        if(cusr == null)return null;
        SmsData data = new SmsData();
        data.set_id(cusr.getString(0));
        data.setThread_id(cusr.getString(1));
        data.setAddress(cusr.getString(2));
        data.setPerson(cusr.getString(3));
        data.setDate(cusr.getString(4));
        data.setDate_sent(cusr.getString(5));
        data.setProtocol(cusr.getString(6));
        data.setRead(cusr.getString(7));
        data.setStatus(cusr.getString(8));
        data.setType(cusr.getString(9));
        data.setReply_path_present(cusr.getString(10));
        data.setSubject(cusr.getString(11));
        data.setBody(cusr.getString(12));
        data.setService_center(cusr.getString(13));
        data.setLocked(cusr.getString(14));
        data.setPhone_id(cusr.getString(15));
        data.setError_code(cusr.getString(16));
        data.setSeen(cusr.getString(17));
        data.setExpired(cusr.getString(18));
        data.setWap_push(cusr.getString(19));
//        data.setIcc_id(cusr.getString(20));
        return data;
    }
    public static ContentValues getContentValues(SmsData smsData){
        if(smsData == null)return null;
        ContentValues contentValues = new ContentValues();
        //contentValues.put("_id" , Utils.getString(smsData.get_id()));
        //contentValues.put("thread_id" , Utils.getString(smsData.getThread_id()));
        contentValues.put("address" , Utils.getString(smsData.getAddress()));
        contentValues.put("person" , Utils.getString(smsData.getPerson()));
        contentValues.put("date" , Utils.getString(smsData.getDate()));
        contentValues.put("date_sent" , Utils.getString(smsData.getDate_sent()));
        contentValues.put("protocol" , Utils.getString(smsData.getProtocol()));
        contentValues.put("read" , Utils.getString(smsData.getRead()));
        contentValues.put("status", Utils.getString(smsData.getStatus()));
        contentValues.put("type" , Utils.getString(smsData.getType()));
        contentValues.put("reply_path_present" , Utils.getString(smsData.getReply_path_present()));
        contentValues.put("subject" , Utils.getString(smsData.getSubject()));
        contentValues.put("body" , Utils.getString(smsData.getBody()));
        contentValues.put("service_center" , Utils.getString(smsData.getService_center()));
        contentValues.put("locked" , Utils.getString(smsData.getLocked()));
        contentValues.put("sub_id" , Utils.getString(smsData.getPhone_id()));
        contentValues.put("error_code" , Utils.getString(smsData.getError_code()));
        contentValues.put("seen" , Utils.getString(smsData.getSeen()));
        contentValues.put("expired" , Utils.getString(smsData.getExpired()));
        contentValues.put("wap_push" , Utils.getString(smsData.getWap_push()));
//        contentValues.put("icc_id" , Utils.getString(smsData.getIcc_id()));
        return contentValues;
    }
}
